*cd "/Users/vetsean/Google Drive/Issue Voting measurement error/Main findings"
*This code creates all possible four-item economic scales
*clear all
/*
global factor 0 // 1 = factor scores vs simple average
global standardized 0 // 1 = means zero, standard deviation one
global missing = . // Set missing value. All coded to 7-point scales, so 4 is midpoint
                     // Need to remain consistent about that when adding new panels
global ktype 1 // 1 normal knowledge coding, 9 place them on the right sides of the scale  
*/
*global studies Econ_SSI1516 All_08  All_92_96   Econ_7276  Econ_BES9295 Econ_BES9296 Econ_BES9297 Econ_BES9701 Econ_76 Econ_94_96 Econ_08 Econ_92_96 Env_08 Immigration_08  Liberties_08
global no_takers 0 // 1 = no takers excluded from knowledge

capture program drop norm					 
program define norm
 summarize  `1'
 replace `1' = (`1'-r(min))/(r(max)-r(min))
 summarize  `1'
end
					 

					 
*********************************************************************
**************SSI 2015-16 panel **********************************************

*stability
 use Data/ssi, clear
*set variables 
global issues foodstamps minwage schools ss medicare housing childcare debtceiling parentleave services
global issues2 gun health affact immigration tax gay climate israel flag
global issues3 foodstamps minwage schools ss medicare housing childcare debtceiling parentleave services gun health affact immigration tax gay climate israel flag
global year_1 1
global year_2 2
global study Econ_SSI1516
global ni 10
g ID = 1516000+ _n  //Numeric  _n
g gk=(soph)

*All issues scaled so higher values are conservative, all on seven-point scale
for any $issues : rename place_X_self$year_1 st_X_$year_1 \ rename place_X_self$year_2 st_X_$year_2
for any $issues : replace st_X_$year_1= st_X_$year_1 * 6+1 \ replace st_X_$year_2=st_X_$year_2 * 6+1
for any $issues :  g missing_X_1 =  st_X_$year_1 == 595 if  st_X_$year_1 !=. \ g missing_X_2 =  st_X_$year_2 == 595 if st_X_$year_2!=.
for any $issues : recode st_X_$year_1 st_X_$year_2 (595 = $missing ) 

destring iss_gun_know2-iss_flag_know2, replace i("NA")

*drop people who correctly placed the candidates in one way but not the other
*rename position knowledge
for any $issues: destring place_X_dem2, ignore("NA") replace \ destring place_X_rep2, ignore("NA") replace 
for any $issues: g lp_X_$year_1 = round((place_X_dem*6)+1,1) \ g lp_X_$year_2 = round((place_X_dem2*6)+1,1) // positions of left candidate/party
for any $issues: g rp_X_$year_1 = round((place_X_rep*6)+1,1) \ g rp_X_$year_2 = round((place_X_rep2*6)+1,1) // positions of right candidate/party
for any $issues: recode lp_X_$year_1 lp_X_$year_2 rp_X_$year_1  rp_X_$year_2 (595=-99) // always use -99 for dk
sum lp_* rp_*
tab1 lp_* rp_*

*imputing missing values
egen not_missing =rownonmiss(st_*_$year_1 st_*_$year_2)
tabulate not_missing,mis
*keep if not_missing >14 // keep 75% or less missing - need to look at tabulate results and set 
tab education,g(ieducation_)
for any $issues :xi: impute st_X_$year_1 ieducation_1-ieducation_8 sex partydum  , generate(s_X_$year_1)
for any $issues :xi: impute st_X_$year_2 ieducation_1-ieducation_8 sex partydum  , generate(s_X_$year_2)

*code party 
tab party 
recode party 1/3 =0 5/7=1  4 8 = .5,g  (PID_1)
recode party 1/3 =0 5/7=1  4 8 = .5,g  (PID_2)
tab party PID_1
g weight=1

*candidate/party approval/feeling thermometer coded to vary between 0-1
g net_feel_1 = ((feel_dem - feel_rep)+4)/8  // I think these are backward coded so that higher values mean less support for the party
g net_feel_2 = ((feel_dem2 -feel_rep2)+4)/8  // dito, so these now have higher values with more support for the conservative party
g therm_1 = net_feel_1
g therm_2 = net_feel_2
g vote_1 = net_feel_1 > 0.5 if net_feel_1 <. // only asked vote for part of the sample in wave 1, so substitute feelings to monomer from wave 6
g vote_2 =net_feel_2 > 0.5 if net_feel_2 <.

corr PID_1 PID_2 net_feel_1 net_feel_2 therm_1 therm_2 feel_rep   feel_dem  feel_rep2 feel_dem2 
sum PID_1 PID_2 net_feel_1 net_feel_2 therm_1 therm_2 feel_rep   feel_dem  feel_rep2 feel_dem2 
do stability_coding.do
sum PID_1 PID_2  therm_1 therm_2 scale_* vote_*
corr PID_1 PID_2 therm_1 therm_2 scale_* vote_*

*********************************************************************
**************all 92-96  policy **********************************************
*stability
 do ANES_92_96.do
*set variables 
global issues gss job def ide abr  
global year_1 92
global year_2 96
global study All_92_96
global ni 5
g ID =  VCASEID*100
g gk=(kg_total)
tab job_92
*for any $issues : sum X_92 X_96 
for any $issues : rename X_92 st_X_92 \ rename X_96 st_X_96 
for any $issues :  g missing_X_1 =  st_X_$year_1 == -99   if st_X_$year_1 !=.  \ g missing_X_2 =  st_X_$year_2 == -99 if st_X_$year_1 !=. 
for any $issues : recode st_X_$year_1 st_X_$year_2 (-99 = $missing ) 
*corr st_*_$year_1
*factor st_*_$year_1,pcf

*placement knowledge
for any $issues: g lp_X_$year_1 = c_X_$year_1 \ g lp_X_$year_2 = c_X_$year_2 // positions of left candidate/party
for any $issues: g rp_X_$year_1 = b_X_$year_1 \ g rp_X_$year_2 = b_X_$year_2 // positions of right candidate/party
for any $issues: recode lp_X_$year_1 lp_X_$year_2 rp_X_$year_1  rp_X_$year_2 (8=-99) // always use -99 for dk
sum lp_* rp_*
tab1 lp_* rp_*
sum st_*_$year_1

*imputing missing values
egen nmmissing =rownonmiss(st_*_$year_1 st_*_$year_2)
tabulate nmmissing,mis
keep if nmmissing >4 // keep 75% or less missing

for any $issues :xi: impute st_X_$year_1  i.V924202 i.V923904 i.V923908   , generate(s_X_$year_1)
for any $issues :xi: impute st_X_$year_2  i.V924202 i.V923904 i.V923908    , generate(s_X_$year_2)

g PID_1 = PID_$year_1 *6+1
g PID_2 = PID_$year_2 *6+1
tab1 PID_*
corr PID_*
recode PID_* (1/3 =0) (4 = 0.5) (5/7 = 1)
g weight=V960004 
g therm_92 = (Bush_92-Clinton_92+1)/2
g therm_96 = (Dole_96-Clinton_96+1)/2 
g vote_1 = vote_92 
g vote_2 =vote_96 
corr vote_1 vote_2 PID_1 PID_2 therm_* s_*
sum vote_1 vote_2 PID_1 PID_2 therm_* s_*
do stability_coding.do


*********************************************************************
**************94-96 three-item economic**********************************************
*stability
 do ANES_92_96.do
*set variables 
global issues gss job health  
global year_1 94
global year_2 96
global study Econ_94_96
global ni 3
g ID =  VCASEID*100
g gk=(kg_total)
tab job_96
for any $issues : rename X_94 st_X_94 \ rename X_96 st_X_96 
for any $issues :  g missing_X_1 =  st_X_$year_1 == -99  if st_X_$year_1 !=.   \ g missing_X_2 =  st_X_$year_2 ==-99 if st_X_$year_2 !=. 
for any $issues : recode st_X_$year_1 st_X_$year_2 (-99 = $missing ) 

*placement knowledge
drop c_gss_94  c_job_94   c_health_94 // drop since we are substituting party
for any $issues: g c_X_94 = d_X_94 \ g b_X_94 = r_X_94  // no candidate placements in 1994 so substitute party placements
for any $issues: g lp_X_$year_1 = c_X_$year_1 \ g lp_X_$year_2 = c_X_$year_2 // positions of left candidate/party
for any $issues: g rp_X_$year_1 = b_X_$year_1 \ g rp_X_$year_2 = b_X_$year_2 // positions of right candidate/party
for any $issues: recode lp_X_$year_1 lp_X_$year_2 rp_X_$year_1  rp_X_$year_2 (8=-99) // always use -99 for dk
tab1 lp_* rp_*
sum lp_* rp_*

*imputing missing values
tab1 V941444 V941434 V941435   
for any $issues :xi: impute st_X_$year_1   i.V941444 i.V941434 i.V941435   , generate(s_X_$year_1)
for any $issues :xi: impute st_X_$year_2  i.V941444 i.V941434 i.V941435    , generate(s_X_$year_2)

g PID_1 = PID_$year_1 *6+1
g PID_2 = PID_$year_2 *6+1
tab1 PID_*
corr PID_*
recode PID_* (1/3 =0) (4 = 0.5) (5/7 = 1)
g weight=V960004 
g therm_94 = (Dole_94-Clinton_92+1)/2
g therm_96 = (Dole_96-Clinton_96+1)/2 

g vote_1 = PID_1 // substitute because no vote in 1994, thermometers are missing for many in 1994
g vote_2 = vote_96 
corr vote_1 vote_2 PID_1 PID_2 therm_* s_*
corr vote_1 vote_2 PID_1 PID_2 therm_* s_*
sum vote_1 vote_2 PID_1 PID_2 therm_* s_*
do stability_coding.do
*********************************************************************

**************Economic 7276**********************************************
*stability
 do anes72_76.do

*set variables 
global issues ins aid job tax 
global year_1 72
global year_2 76
global study Econ_7276
global ni 4
recode V720002 0 =. 
g ID = 7276000+  V720002
g gk=knw_total
tabulate job76
for any $issues : rename X72  st_X_72 \ rename X76 st_X_76 
for any $issues :  g missing_X_1 =  st_X_$year_1 == -99  if st_X_$year_1 !=. \ g missing_X_2 =  st_X_$year_2 == -99  if st_X_$year_2 !=. 
for any $issues : recode st_X_$year_1 st_X_$year_2 (-99 = $missing ) 
sum  st_*
corr  st_* 
tab1  st_tax_72   st_tax_76,mis
corr  st_tax_72   st_tax_76,
 
*placement knowledge -unfortunately, the party questions were on opposite forms,
for any $issues: g lp_X_$year_1 = d_X$year_1 \ g lp_X_$year_2 = d_X$year_2 // positions of left candidate/party
for any $issues: g rp_X_$year_1 = r_X$year_1 \ g rp_X_$year_2 = r_X$year_2 // positions of right candidate/party
for any $issues: recode lp_X_$year_1 lp_X_$year_2 rp_X_$year_1  rp_X_$year_2 (8=-99) (0 9 =.) // always use -99 for dk
sum lp_* rp_*
tab  lp_tax_72   lp_tax_76,mis
tab  lp_tax_72   rp_tax_72,mis

*imputing missing values
for any $issues :xi: impute st_X_$year_1 i.V720306 i.V720387 i.V720420  , generate(s_X_$year_1)
for any $issues :xi: impute st_X_$year_2  i.V720306 i.V720387 i.V720420    , generate(s_X_$year_2)

g PID_1 = pid$year_1 *6+1
g PID_2 = pid$year_2 *6+1
tab1 PID_*
corr PID_*
recode PID_* (1/3 =0) (4 = 0.5) (5/7 = 1)
g weight=1 // no real weight variable 
g vote_1 =pvote72 // neither of these include pre-election vote for those not interviewed in the postelection
g vote_2 =pvote76
corr vote_1 vote_2 PID_1 PID_2 therm_* s_*
corr vote_1 vote_2 PID_1 PID_2  st_*72   st_tax_76
sum vote_1 vote_2 PID_1 PID_2 therm_* s_* lp_*
reg  s_* lp_*
alpha   knw_* pterm sterm  cterm hspre  hspost int_average if e(sample)==1
do stability_coding.do

*********************************************************************
**************Patterson**********************************************
*stability
 do Patterson_v0_7.do
*set variables 
global issues jobs price spd tax //unfortunately they don't ask about price control positions in wave 1
global year_1 3
global year_2 5
global study Econ_76
global ni 4

g ID = 7676000 + _n
*g gk =tgk

tab jobs_5 jobs_3, mis
for any $issues : recode X_3 (0 9=.) (8 = -99), gen(st_X_3) \ recode X_5 (0 9=.) (8 = -99), gen(st_X_5) 
for any $issues :  drop X_3 \ drop X_5 
replace st_tax_3 = 8 - st_tax_3 if st_tax_3 > -99  // reverse code so higher values are more conservative
replace st_tax_5 = 8 - st_tax_5 if st_tax_5 > -99   // reverse code so higher values are more conservative
for any $issues :  g missing_X_1 =  st_X_$year_1 == -99  if st_X_$year_1 !=. \ g missing_X_2 =  st_X_$year_2 == -99  if st_X_$year_2 !=. 
for any $issues : recode st_X_$year_1 st_X_$year_2 (-99 = $missing ) 

*placement knowledge
for any c f : replace tax_X_3 = 8 - tax_X_3 if tax_X_3 >0 & tax_X_3 <8  // reverse code so higher values are more conservative
for any c f : replace tax_X_5 = 8 - tax_X_5 if tax_X_5 >0 & tax_X_5 <8  // reverse code so higher values are more conservative
for any $issues: g lp_X_$year_1 = X_c_$year_1 \ g lp_X_$year_2 = X_c_$year_2 // positions of left candidate/party
for any $issues: g rp_X_$year_1 = X_f_$year_1 \ g rp_X_$year_2 = X_f_$year_2 // positions of right candidate/party
for any $issues: recode lp_X_$year_1 lp_X_$year_2 rp_X_$year_1  rp_X_$year_2 (8=-99) (0 9 =.) // always use -99 for dk
tab1 lp_* rp_*
sum lp_* rp_*

*imputing missing values
for any $issues :xi: impute st_X_$year_1 i.employed i.religion i.rincome  , generate(s_X_$year_1)
for any $issues :xi: impute st_X_$year_2  i.employed i.religion i.rincome  , generate(s_X_$year_2)

replace PID_1 = PID_$year_1  *6+1
replace PID_2 = PID_$year_2  *6+1
tab1 PID_*
corr PID_*

recode PID_1 PID_2 (1/3 =0) (4 = 0.5) (5/7 = 1)
drop PID_3 PID_4 PID_5

capture drop therm_1 therm_2

replace weight=weight/100
g vote_1 =vote_4 // No vote question in wave 3, I think
g vote_2 =vote_5
corr vote_1 vote_2 PID_1 PID_2 therm_* s_*
sum vote_1 vote_2 PID_1 PID_2 therm_* s_*
sum st_*
for any $issues: tabulate  lp_X_$year_1  rp_X_$year_1 
for any $issues: tabulate  lp_X_$year_2 rp_X_$year_2 
for any $issues: g test1_X=  lp_X_$year_1 < rp_X_$year_1 &lp_X_$year_1 != -99& rp_X_$year_1 != -99 if lp_X_$year_1 !=.& rp_X_$year_1 != .
for any $issues: g test2_X=  lp_X_$year_2 < rp_X_$year_2 &lp_X_$year_2 != -99& rp_X_$year_2 != -99 if lp_X_$year_2 !=.& rp_X_$year_2 != .
sum test*_*
egen total_test =rowtotal(test*_*)
tabulate total_test
do stability_coding.do
*********************************************************************
**************British election study 97-01**********************************************
do "BES 1997-2001_v2.do"  //only 97 and 01 waves asked all four economic questions and placements
*set variables 
global issues jbp txs prn ieq  // first three asked every wave, ieq not asked in 1994
global year_1 97
global year_2 01
global study Econ_BES9701
global ni 4

g ID = 9701000 +serialno
g gk=(general_knowledge)
for any $issues :  sum s_X_$year_1 s_X_$year_2 
for any $issues :  g missing_X_1 =  s_X_$year_1 == -99 if s_X_$year_1 !=. \ g missing_X_2 =  s_X_$year_2 == -99 if s_X_$year_2 !=. 
for any $issues : g st_X_$year_1= (s_X_$year_1-1)*.6+1 if s_X_$year_1 >-99 \ drop s_X_$year_1
for any $issues : g st_X_$year_2= (s_X_$year_2-1)*.6+1 if s_X_$year_2 >-99 \ drop s_X_$year_2

*placement knowledge
for any $issues: g lp_X_$year_1 = labX$year_1 \ g lp_X_$year_2 = labX$year_2 // positions of left candidate/party
for any $issues: g rp_X_$year_1 = conX$year_1 \ g rp_X_$year_2 = conX$year_2 // positions of right candidate/party
for any $issues: recode lp_X_$year_1 lp_X_$year_2 rp_X_$year_1  rp_X_$year_2  ( -8/-1  99=.) (12 = 1) (13 =11) ( 98 = -99) // always use -99 for dk
for any $issues : replace lp_X_$year_1= (lp_X_$year_1-1)*.6+1 \ replace lp_X_$year_2= (lp_X_$year_2-1)*.6+1 // convert to seven-point scale
for any $issues : replace rp_X_$year_1= (rp_X_$year_1-1)*.6+1 \ replace rp_X_$year_2= (rp_X_$year_2-1)*.6+1 // convert to seven-point scale
for any $issues: recode lp_X_$year_1 lp_X_$year_2 rp_X_$year_1  rp_X_$year_2  ( -59 = -99) // always use -99 for dk
tab1 lp_* rp_*
sum lp_* rp_*
*imputing missing values
for any $issues :xi: impute st_X_$year_1 i.hedqul97 i.hedqul01 i.unempi97 , generate(s_X_$year_1)
for any $issues :xi: impute st_X_$year_2  i.hedqul97 i.hedqul01 i.unempi97 , generate(s_X_$year_2)

g PID_1 = PID_$year_1
g PID_2 = PID_$year_2 
corr PID_1 st_*_97 therm_97
corr PID_2 st_*_01 therm_01
sum PID_1 PID_2 therm_* s_*
g vote_1 =vote_97 // No vote question in wave 3, I think
g vote_2 =vote_01
corr vote_1 vote_2 PID_1 PID_2 therm_* s_*
sum vote_1 vote_2 PID_1 PID_2 therm_* s_*

do stability_coding.do
*********************************************************************
**************British election study**********************************************
 do BES_0_2.do
*set variables 
global issues jbp txs prn ieq  // first three asked every wave, ieq not asked in 1994
global year_1 92
global year_2 95
global study Econ_BES9295
global ni 4
g ID = 9297000 + serialno
g gk=(general_knowledge)
for any $issues :  g missing_X_1 =  s_X_$year_1 == -99  if s_X_$year_1 !=.  \ g missing_X_2 =  s_X_$year_2 == -99  if s_X_$year_2 !=. 
for any $issues : g st_X_$year_1= (s_X_$year_1-1)*.6+1 if s_X_$year_1 >-99  \ drop s_X_$year_1  // convert to seven-point scale
for any $issues : g st_X_$year_2= (s_X_$year_2-1)*.6+1 if s_X_$year_2 >-99  \ drop s_X_$year_2 // convert to seven point scale

*placement knowledge
for any $issues: g lp_X_$year_1 = labX$year_1 \ g lp_X_$year_2 = labX$year_2 // positions of left candidate/party
for any $issues: g rp_X_$year_1 = conX$year_1 \ g rp_X_$year_2 = conX$year_2 // positions of right candidate/party
for any $issues: recode lp_X_$year_1 lp_X_$year_2 rp_X_$year_1  rp_X_$year_2  ( -8/-1  99=.) (12 = 1) (13 =11) (97 98 = -99) // always use -99 for dk
for any $issues : replace lp_X_$year_1= (lp_X_$year_1-1)*.6+1 \ replace lp_X_$year_2= (lp_X_$year_2-1)*.6+1 \ replace rp_X_$year_1= (rp_X_$year_1-1)*.6+1 \ replace rp_X_$year_2= (rp_X_$year_2-1)*.6+1 // convert to seven-point scale
for any $issues: recode lp_X_$year_1 lp_X_$year_2 rp_X_$year_1  rp_X_$year_2  ( -59 = -99) // always use -99 for dk
tab1 lp_* rp_*
sum lp_* rp_*
*imputing missing values
for any $issues :xi: impute st_X_$year_1 i.hhinc92 i.unempi92 i.rsc92 i.ssc92 , generate(s_X_$year_1)
for any $issues :xi: impute st_X_$year_2 i.hhinc92 i.unempi92 i.rsc92 i.ssc92 , generate(s_X_$year_2)

g PID_1 = PID_$year_1
g PID_2 = PID_$year_2 
corr PID_1 s_*_92 therm_92
corr PID_2 s_*_95 therm_95
sum  PID_1 s_*_92 therm_92 PID_2 s_*_95 therm_95
g vote_1 =vote_92 // No vote question in wave 3, I think
g vote_2 =vote_95
corr vote_1 vote_2 PID_1 PID_2 therm_* s_*
sum vote_1 vote_2 PID_1 PID_2 therm_* s_*
do stability_coding.do

*********************************************************************
**************British election study**********************************************
 do BES_0_2.do
*set variables 
global issues jbp txs prn ieq  // first three asked every wave, ieq not asked in 1994
global year_1 92
global year_2 96
global study Econ_BES9296
global ni 4

g ID = 9297000 +serialno
g gk=(general_knowledge)

for any $issues : tab s_X_$year_1
for any $issues :  g missing_X_1 =  s_X_$year_1 == -99  \ g missing_X_2 =  s_X_$year_2 == -99
for any $issues : g st_X_$year_1= (s_X_$year_1-1)*.6+1 if s_X_$year_1 >-99 \ drop s_X_$year_1
for any $issues : g st_X_$year_2= (s_X_$year_2-1)*.6+1 if s_X_$year_2 >-99 \ drop s_X_$year_2

*placement knowledge
for any $issues: g lp_X_$year_1 = labX$year_1 \ g lp_X_$year_2 = labX$year_2 // positions of left candidate/party
for any $issues: g rp_X_$year_1 = conX$year_1 \ g rp_X_$year_2 = conX$year_2 // positions of right candidate/party
for any $issues: recode lp_X_$year_1 lp_X_$year_2 rp_X_$year_1  rp_X_$year_2  ( -8/-1  99=.) (12 = 1) (13 =11) (97 98 = -99) // always use -99 for dk
for any $issues : replace lp_X_$year_1= (lp_X_$year_1-1)*.6+1 \ replace lp_X_$year_2= (lp_X_$year_2-1)*.6+1 \ replace rp_X_$year_1= (rp_X_$year_1-1)*.6+1 \ replace rp_X_$year_2= (rp_X_$year_2-1)*.6+1 // convert to seven-point scale
for any $issues: recode lp_X_$year_1 lp_X_$year_2 rp_X_$year_1  rp_X_$year_2  ( -59 = -99) // always use -99 for dk
tab1 lp_* rp_*
sum lp_* rp_*
*imputing missing values
for any $issues :xi: impute st_X_$year_1 i.hhinc92 i.unempi92 i.rsc92 i.ssc92 , generate(s_X_$year_1)
for any $issues :xi: impute st_X_$year_2 i.hhinc92 i.unempi92 i.rsc92 i.ssc92 , generate(s_X_$year_2)

g PID_1 = PID_$year_1
g PID_2 = PID_$year_2 
corr PID_1 s_*_92 therm_92
corr PID_2 s_*_96 therm_96
sum  PID_1 s_*_92 therm_92 PID_2 s_*_96 therm_96
g vote_1 =1-vote_92 // No vote question in wave 3, I think
g vote_2 =1-vote_96
corr vote_1 vote_2 PID_1 PID_2 therm_* s_*
sum vote_1 vote_2 PID_1 PID_2 therm_* s_*

do stability_coding.do
centile gk  , centile(20 40 60 80)
xtile gk_5 = gk ,n(5)
tab gk_5

*********************************************************************
**************British election study**********************************************
*Does general knowledge of positions or specific knowledge of positions better explain
*stability
 do BES_0_2.do
*set variables 
global issues jbp txs prn ieq  // first three asked every wave, ieq not asked in 1994
global year_1 92
global year_2 97
global study Econ_BES9297
global ni 4

g ID = 9297000 +serialno
g gk=(general_knowledge)
for any $issues : tab s_X_$year_1
for any $issues :  g missing_X_1 =  s_X_$year_1 == -99  \ g missing_X_2 =  s_X_$year_2 == -99
for any $issues : g st_X_$year_1= (s_X_$year_1-1)*.6+1 if s_X_$year_1 >-99 \ drop s_X_$year_1
for any $issues : g st_X_$year_2= (s_X_$year_2-1)*.6+1 if s_X_$year_2 >-99 \ drop s_X_$year_2
for any $issues : sum st_X_$year_1 st_X_$year_2 

*placement knowledge
for any $issues: g lp_X_$year_1 = labX$year_1 \ g lp_X_$year_2 = labX$year_2 // positions of left candidate/party
for any $issues: g rp_X_$year_1 = conX$year_1 \ g rp_X_$year_2 = conX$year_2 // positions of right candidate/party
for any $issues: recode lp_X_$year_1 lp_X_$year_2 rp_X_$year_1  rp_X_$year_2  ( -8/-1  99=.) (12 = 1) (13 =11) (97 98 = -99) // always use -99 for dk
for any $issues : replace lp_X_$year_1= (lp_X_$year_1-1)*.6+1 \ replace lp_X_$year_2= (lp_X_$year_2-1)*.6+1 \ replace rp_X_$year_1= (rp_X_$year_1-1)*.6+1 \ replace rp_X_$year_2= (rp_X_$year_2-1)*.6+1 // convert to seven-point scale
for any $issues: recode lp_X_$year_1 lp_X_$year_2 rp_X_$year_1  rp_X_$year_2  ( -59 = -99) // always use -99 for dk
tab1 lp_* rp_*
sum lp_* rp_*
*imputing missing values
for any $issues :xi: impute st_X_$year_1 i.hhinc92 i.unempi92 i.rsc92 i.ssc92 , generate(s_X_$year_1)
for any $issues :xi: impute st_X_$year_2 i.hhinc92 i.unempi92 i.rsc92 i.ssc92 , generate(s_X_$year_2)

g PID_1 = PID_$year_1
g PID_2 = PID_$year_2 
corr PID_1 s_*_92 therm_92
corr PID_2 s_*_97 therm_97
sum  PID_1 s_*_92 therm_92 PID_2 s_*_97 therm_97
g vote_1 =1-vote_92 // No vote question in wave 3, I think
g vote_2 =1-vote_97
corr vote_1 vote_2 PID_1 PID_2 therm_* s_*
sum vote_1 vote_2 PID_1 PID_2 therm_* s_*

do stability_coding.do

****************COMBINE FILES******************************************* BES_66_70
global studies2   Econ_BES9295 Econ_BES9296 Econ_BES9297 Econ_BES9701 Econ_76 Econ_94_96 All_92_96 Econ_SSI1516
use Data/temporary/stability_pooled_Econ_7276, clear
for any $studies2: append using Data/temporary/stability_pooled_X, 
g k_percent =k_scale/NUM_issues
g gk_5 =.
for any $studies :xtile gk_X_5 = gk if study == "X",n(5)
for any $studies :replace  gk_5 =gk_X_5 if study == "X" \ drop gk_X_5
tab study gk_5
g gk_10 =.
for any $studies :xtile gk_X_10 = gk if study == "X",n(10)
for any $studies :replace  gk_10 =gk_X_10 if study == "X" \ drop gk_X_10
g gkn =.
for any $studies : sum gk if study == "X" \  replace gkn = (gk-r(min))/(r(max)-r(min)) if study == "X"
g independent = PID_1 ==.5 | PID_2 == .5 if PID_1!=. & PID_2!=.
g partisan =independent == 0 if independent !=.
g stable_PID =PID_1 == PID_2  if PID_1!=. & PID_2!=.
for any $studies: egen therm_1X=std(therm_1)  if study == "X"
g scale_therm=.
for any $studies: replace scale_therm= abs(scale_1-therm_1X)  if study == "X" \ drop therm_1X
g therm_switch= ((therm_1<4 & therm_2 >4 & therm_2 <.)  | (therm_1>4 & therm_2<4 & therm_1 <.) ///
              | (therm_1==4 & therm_2!=4 & therm_2 <.)| (therm_2== 4 & therm_1!=4 & therm_1 <.)) ///
			  if (regexm(study,"Econ_76")==1 | study== "Econ_7276")
replace therm_switch= ((therm_1<7 & therm_2 >7 & therm_2 <.)  | (therm_1>7 & therm_2<7 & therm_1 <.) ///
              | (therm_1==7 & therm_2!=7 & therm_2 <.)| (therm_2== 7 & therm_1!=7 & therm_1 <.)) ///
			  if regexm(study,"BES") == 1
recode therm_switch	 1 = 0 0 = 1,gen( therm_stable)	  

tab study,g(study_n)

replace total_knowledge= round(total_knowledge,1)
for any ins aid job jobs price spd tax jbp txs prn ieq:g issue_X = regex(issues_scale, "X")
g agree_candidate = scale_1>4 & therm_1 > 0.5 | scale_1<4 & therm_1<0.5  if scale_1 <. & scale_2 <. & PID_1<.
g agree_party = scale_1>4 & PID_1==1 | scale_1<4 & PID_1==0  if scale_1 <. & scale_2 <. & PID_1<.
g agree = agree_candidate == 1 | agree_party == 1 if agree_candidate<. | agree_party<.

*only three individuals in 2008 placed all the candidates correctly (seven correct) , so
egen within_SD =rowsd(scale_1 scale_2)

bys study: tab total_knowledge if NUM_issues == 1
replace total_knowledge = 6 if total_knowledge == 7 & study =="All_08"
replace total_knowledge = 6 if total_knowledge == 7 & study =="All_7276"
replace total_knowledge=0 if study =="Econ_SSI1516" & total_knowledge==0
replace total_knowledge=1 if study =="Econ_SSI1516" & (total_knowledge==1 |total_knowledge==2 |total_knowledge==3 )
replace total_knowledge=2 if study =="Econ_SSI1516" & (total_knowledge==4 |total_knowledge==5|total_knowledge==6  )
replace total_knowledge=3 if study =="Econ_SSI1516" & total_knowledge>=7 & total_knowledge<= 10 

g total_issues2 = total_issues
replace total_issues2 = 3 if study =="Econ_SSI1516"
replace total_issues2 = 6 if study =="All_7276"
replace total_issues2 = 3 if study =="Econ_76"

g placement_percent = round(total_knowledge/total_issues2*100, 25)
g tk_percent = round(total_knowledge/total_issues2*100 , 25)
*these should all be roughly 1-7:
 sum scale_* 
 bys study: sum scale_* 
 
*create a attitude response with no variation across individuals
egen scale_mean = mean((scale_1+scale_2)/2), by(study )
egen scale_SD = sd((scale_1+scale_2)/2), by(study )
replace scale_SD = 1 if scale_SD <.1
*egen scale_2_mean = mean(scale_2), by(study ID)
*egen scale_2_SD = sd(scale_2), by(study ID)
g scale_1_s = (scale_1 - scale_mean)/scale_SD
g scale_2_s = (scale_2 - scale_mean)/scale_SD
 sum scale_*
 
 
label variable k_scale "Number of correct placements in scale"
label variable total_knowledge "Total number of issues correct"
label variable NUM_issues "Number of issues in scale"
label variable k_percent "Correctly placed issue % in scale"
 label variable gk_5 "General knowledge quintiles"


*these should all be 0-1:
 sum  therm_1 therm_2 PID_1 PID_2
 bys study: sum therm_1 therm_2 
 compress
save Data/stability_pooled_std${standardized}ktype${ktype}fct${factor} , replace

*************************************************************	
*Correlations
*************************************************************

*Number of issues 
use Data/stability_pooled_std${standardized}ktype${ktype}fct${factor}, clear	
sum scale_1 scale_2 total_knowledge  k_scale
tempname memhold
postfile `memhold' t corr N ni  str17 study  using Data/stability_pooled_corr_ARS_std${standardized}ktype${ktype}fct${factor}, replace	
foreach s in $studies {
	disp "`s'"
    sum t if study == "`s'"
    local z = r(max)
	forval i=1/`z' { // tupples
		  sum NUM_issues if  t==`i' & study == "`s'"
		  local w = r(mean)
		  capture corr scale_1 scale_2  if  t==`i' & study == "`s'"
		   if _rc==0 {
			 post `memhold' (`i') (r(rho)) (r(N)) (`w') ("`s'") 
		   }
	}
}
postclose `memhold'
